package de.tum.in.eist.poll.adapter;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.VerticalPanel;

import de.tum.in.eist.poll.controller.FeedbackController;
/**
 * The feedback adapter
 * handles feedback input
 * @author sudi
 *
 */
public class FeedbackAdapter {
	// feedback Form
	/**
	 * feedback panels
	 */
	private VerticalPanel feedbackForm = new VerticalPanel();
	private Label choiceLabel = new Label(
			"How satisfied are you with today's lecture?");
	private FlowPanel choicePanel = new FlowPanel();
	
	/**
	 * The rating buttons
	 */
	private RadioButton radioButton1 = new RadioButton("Rating",
			"Very unsatisfied");
	private RadioButton radioButton2 = new RadioButton("Rating", "Unsatisfied");
	private RadioButton radioButton3 = new RadioButton("Rating", "Neutral");
	private RadioButton radioButton4 = new RadioButton("Rating", "Satisfied");
	private RadioButton radioButton5 = new RadioButton("Rating",
			"Very Satisfied");
	
	/**
	 * The like/dislike texts
	 */
	private Label likeLabel = new Label("What did you like most?");
	private TextArea likeText = new TextArea();
	private Label dislikeLabel = new Label(
			"What did you dislike or did not understand at all?");
	private TextArea dislikeText = new TextArea();
	
	/**
	 * send and cancel feedback
	 */
	private Button sendButton = new Button("Send");
	private Button cancelButton = new Button("Cancel");
	private DialogBox feedbackDialog;
	private FeedbackController feedbackController;
	
	public FeedbackAdapter(FeedbackController feedbackController) {
		this.feedbackController = feedbackController;
	}
	
	/**
	 * creates the feedback view
	 */
	public void create(){
		choiceLabel.addStyleName("boldLabel");
		likeLabel.addStyleName("boldLabel");
		dislikeLabel.addStyleName("boldLabel");
		likeText.addStyleName("feedbackTextBox");
		dislikeText.addStyleName("feedbackTextBox");
		
		feedbackForm.add(choiceLabel);
		choicePanel.add(radioButton1);
		choicePanel.add(radioButton2);
		choicePanel.add(radioButton3);
		choicePanel.add(radioButton4);
		choicePanel.add(radioButton5);
		feedbackForm.add(choicePanel);	
		feedbackForm.add(likeLabel);
		feedbackForm.add(likeText);
		feedbackForm.add(dislikeLabel);
		feedbackForm.add(dislikeText);
		HorizontalPanel buttonPanel = new HorizontalPanel();
		buttonPanel.add(sendButton);
		buttonPanel.setSpacing(10);
		buttonPanel.add(cancelButton);
		feedbackForm.add(buttonPanel);
		
		feedbackForm.setSpacing(5);
		
		feedbackDialog = new DialogBox();

		feedbackDialog.add(feedbackForm);
		
		feedbackDialog.center();
		
		PressEnterListener enterListener = new PressEnterListener();
		likeText.addKeyPressHandler(enterListener);
		dislikeText.addKeyPressHandler(enterListener);
		
		sendButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				int satisfaction = -1;
				
				if (radioButton1.getValue())
					satisfaction = 0;
				else if (radioButton2.getValue())
					satisfaction = 1;
				else if (radioButton3.getValue())
					satisfaction = 2;
				else if (radioButton4.getValue())
					satisfaction = 3;
				else if (radioButton5.getValue())
					satisfaction = 4;
				else{
					Window.alert("Please rate the lecture.");
					return;
				}
				
				feedbackController.sendFeedback(satisfaction, likeText.getText().trim(), dislikeText.getText().trim());
				feedbackDialog.removeFromParent();
			}
		});
		cancelButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				History.back();
			}
		});
	}
	
	/**
	 * key press listener to catch the enter key for submitting
	 * @author sudi
	 *
	 */
	class PressEnterListener implements KeyPressHandler {

		public void onKeyPress(KeyPressEvent event) {
			if (event.getCharCode() == KeyCodes.KEY_ENTER) {
				int satisfaction = -1;
				
				if (radioButton1.getValue())
					satisfaction = 0;
				else if (radioButton2.getValue())
					satisfaction = 1;
				else if (radioButton3.getValue())
					satisfaction = 2;
				else if (radioButton4.getValue())
					satisfaction = 3;
				else if (radioButton5.getValue())
					satisfaction = 4;
				else{
					Window.alert("Please rate the lecture.");
					return;
				}
				
				feedbackController.sendFeedback(satisfaction, likeText.getText().trim(), dislikeText.getText().trim());
				feedbackDialog.removeFromParent();
			}
		}
	}
}